package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Sports;
import com.itheima.service.FoodService;
import com.itheima.service.SportsService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HLJ
 */
@RestController
@RequestMapping("/sports")
public class SportsController {
    @Reference
    private SportsService sportsService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile) throws IOException {
        List<String[]> list = POIUtils.readExcel(excelFile);
        List<Sports> sports = new ArrayList<>();
        try {
            if (list != null && list.size()>0){
                for (String[] strings : list) {
                    Integer id = Integer.valueOf(strings[0].trim());
                    String sportsId = strings[1];
                    String sportsName = strings[2];
                    Integer sportsRisk = Integer.valueOf(strings[3].trim());
                    Integer sportsType = Integer.valueOf(strings[4].trim());
                    String sportsTime = strings[5];
                    String sportsFre = strings[6].trim();
                    Integer minAge = Integer.valueOf(strings[7].trim());
                    Integer maxAge = Integer.valueOf(strings[8].trim());
                    String actionParts = strings[9];
                    String actionMethod = strings[10];
                    Integer isStart = Integer.valueOf(strings[11].trim());
                    Float calorie = Float.valueOf(strings[12].trim());
                    Float energy = Float.valueOf(strings[13].trim());
                    Sports sport = new Sports(id,sportsId,sportsName,sportsRisk,sportsType,sportsTime,sportsFre,minAge,maxAge,actionParts,actionMethod,isStart,calorie,energy);
                    sports.add(sport);
                }
            }
            sportsService.upload(sports);
            return new Result(true,MessageConstant.UPLOAD_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.UPLOAD_FAIL);
        }

    }

    @RequestMapping("/findPage")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult page = sportsService.findPage(queryPageBean);
        return page;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Sports sports = sportsService.findById(id);
        return new Result(true,MessageConstant.QUERY_SPORTS_SUCCESS,sports);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Sports sports){
        try {
            sportsService.add(sports);
            return new Result(true, MessageConstant.ADD_SPORTS_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SPORTS_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Sports sports){
        try {
            sportsService.edit(sports);
            return new Result(true,MessageConstant.EDIT_SPORTS_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SPORTS_FAIL);
        }
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            sportsService.delete(id);
            return new Result(true,MessageConstant.DELETE_SPORTS_SUCCESS);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SPORTS_FAIL);
        }

    }
}
